package com.jcl.recruit.user.mapper;

import com.jcl.recruit.user.model.User;
import com.jcl.core.utils.StringUtils;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class UserSqlProvider {

    public String insertSelective(User record) {
        BEGIN();
        INSERT_INTO("user");

        if (record.getId() != null) {
            VALUES("id", "#{id,jdbcType=INTEGER}");
        }

        if (record.getDepartId() != null) {
            VALUES("depart_id", "#{departId,jdbcType=INTEGER}");
        }

        if (record.getWorkCode() != null) {
            VALUES("work_code", "#{workCode,jdbcType=VARCHAR}");
        }

        if (record.getPostCode() != null) {
            VALUES("post_code", "#{postCode,jdbcType=VARCHAR}");
        }

        if (record.getRealname() != null) {
            VALUES("realname", "#{realname,jdbcType=VARCHAR}");
        }
        
        if (record.getUsername() != null) {
            VALUES("username", "#{username,jdbcType=VARCHAR}");
        }
        
        if (record.getPassword() != null) {
            VALUES("password", "#{password,jdbcType=VARCHAR}");
        }
        
        if (record.getEmail() != null) {
            VALUES("email", "#{email,jdbcType=VARCHAR}");
        }
        
        if (record.getPostName() != null) {
            VALUES("post_name", "#{postName,jdbcType=VARCHAR}");
        }

        if (record.getHr() != null) {
            VALUES("is_hr", "#{isHr,jdbcType=BIT}");
        }

        if (StringUtils.hasLength(record.getRoles())) {
            VALUES("roles", "#{roles,jdbcType=VARCHAR}");
        }

        if (record.getCorpCode() != null) {
            VALUES("corp_code", "#{corpCode,jdbcType=VARCHAR}");
        }

        if (record.getStatus() != null) {
            VALUES("status", "#{status,jdbcType=INTEGER}");
        }
        
        if (record.getCreateTime() != null) {
            VALUES("create_time", "#{createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getToken() != null) {
            VALUES("token", "#{token,jdbcType=VARCHAR}");
        }
        
        return SQL();
    }

    public String updateByPrimaryKeySelective(User record) {
        BEGIN();
        UPDATE("user");

        if (record.getDepartId() != null) {
            SET("depart_id = #{departId,jdbcType=INTEGER}");
        }

        if (record.getWorkCode() != null) {
            SET("work_code = #{workCode,jdbcType=VARCHAR}");
        }

        if (record.getPostCode() != null) {
            SET("post_code = #{postCode,jdbcType=VARCHAR}");
        }

        if (record.getRealname() != null) {
            SET("realname = #{realname,jdbcType=VARCHAR}");
        }

        if (record.getUsername() != null) {
            SET("username = #{username,jdbcType=VARCHAR}");
        }

        if (record.getPassword() != null) {
            SET("password = #{password,jdbcType=VARCHAR}");
        }

        if (record.getEmail() != null) {
            SET("email = #{email,jdbcType=VARCHAR}");
        }

        if (record.getPostName() != null) {
            SET("post_name = #{postName,jdbcType=VARCHAR}");
        }

        if (record.getHr() != null) {
            SET("is_hr = #{isHr,jdbcType=BIT}");
        }

        if (StringUtils.hasLength(record.getRoles())) {
            SET("roles = #{roles,jdbcType=VARCHAR}");
        }

        if (record.getCorpCode() != null) {
            SET("corp_code = #{corpCode,jdbcType=VARCHAR}");
        }

        if (record.getStatus() != null) {
            SET("status = #{status,jdbcType=INTEGER}");
        }

        if (record.getCreateTime() != null) {
            SET("create_time = #{createTime,jdbcType=TIMESTAMP}");
        }

        if (record.getToken() != null) {
            SET("token = #{token,jdbcType=VARCHAR}");
        }
        
        WHERE("id = #{id,jdbcType=INTEGER}");
        
        return SQL();
    }
}